<template>
  <div class="app-views">
    <div
      class="item-box"
      v-loading="loading"
      style="border-right: 1px dashed #e6e6e6"
    >
      <div class="sub-title">
        <i
          class="iconfont icon-list"
          style="margin-right: 5px; font-weight: bold"
        ></i>
        <div class="Title">设备列表</div>
      </div>
      <div class="device-list">
        <DCard
          :dName="item.name"
          :isCheck="isCheckCard == item.value"
          @click.native="handlerisCheck(item)"
          v-for="(item, index) in temperAfter"
          :key="index"
        />
      </div>
    </div>
    <div class="device-views">
      <div class="sub-title">
        <i class="iconfont icon-shebeituceng-" style="margin-right: 5px"></i>
        <div class="Title">设备图</div>
      </div>
      <div class="device-img">
        <div class="device-img-rec">
          <div class="img-box">
            <img src="@/assets/images/temperAfter.png" alt="" />
            <Client :isPhoto="true" left="45%" top="12%" :PostionNumber="211" />
            <Client :isPhoto="true" left="45%" top="36%" :PostionNumber="221" />

            <Client
              :isPhoto="true"
              left="45%"
              top="55%"
              :PostionNumber="cameraList[8]"
            />
            <Client
              :isPhoto="true"
              left="45%"
              top="68%"
              :PostionNumber="cameraList[9]"
            />
            <Client
              :isPhoto="true"
              left="45%"
              top="81%"
              :PostionNumber="cameraList[10]"
            />
          </div>
          <div class="deviceStatus">
            <div class="title">
              设备状态
              <i
                class="el-icon-edit"
                @click="openParamSet"
                v-hasPermi="['assembly:device:edit']"
              ></i>
            </div>
            <div class="list">
              <div class="item" v-for="item in showDeviceStatus" :key="item.id">
                <div class="descript">
                  <span class="note"> {{ item.note }}</span>
                  <span class="value">
                    {{ item.message }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="signal">
          <div class="sub-title">
            <i
              class="iconfont icon-PLCxinhaoganzhizhongduan"
              style="margin-right: 5px"
            ></i>
            <div class="Title">信号列表</div>
          </div>
          <div class="signal-table">
            <el-table :data="tableData" style="width: 100%" height="100%">
              <el-table-column prop="id" label="ID" width="80" />
              <el-table-column
                prop="cmd"
                label="指令内容"
                width="200"
                show-overflow-tooltip
              />
              <el-table-column
                prop="cmdType"
                label="命今类型"
                show-overflow-tooltip
              />
              <el-table-column prop="createTime" label="时间" width="150">
                <template slot-scope="scope">
                  {{ scope.row.createTime.substr(10) }}
                </template>
              </el-table-column>
              <el-table-column label="完成状态" width="100">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.state | tagType">{{
                    scope.row.state | status
                  }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                  <el-button
                    icon="el-icon-close"
                    type="text"
                    v-if="!scope.row.state"
                    v-hasPermi="['assembly:device:edit']"
                    @click="handlerCmd('fallbackAction', scope.row)"
                    >取消</el-button
                  >
                  <el-button
                    icon="el-icon-check"
                    type="text"
                    v-if="!scope.row.state"
                    v-hasPermi="['assembly:device:edit']"
                    @click="handlerCmd('executionCompleted', scope.row)"
                    >完成</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <div class="item-box">
      <LogView :jobGroup="9" :isShow="isShow" />
    </div>
    <el-drawer title="设备参数" :visible.sync="drawer" :direction="direction">
      <div
        class="params"
        v-if="deviceParams != null"
        v-loading="loadDeviceParam"
      >
        <div class="param_item_W">
          <div class="paramsName">默认格拉斯通优先</div>
          <div class="paramView">
            <el-radio-group v-model="deviceParams.object.allowGlstFirst">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37强制进入下个台子</div>
          <div class="paramView">
            <el-radio-group v-model="deviceParams.object.forceA37TONext">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A361强制进入A39</div>
          <div class="paramView">
            <el-radio-group v-model="deviceParams.object.forceA361TOA39">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">接收台子</div>
          <div class="paramView">
            <el-checkbox-group v-model="deviceParams.object.canSendLine">
              <el-checkbox :label="1">一线</el-checkbox>
              <el-checkbox :label="2">二线</el-checkbox>
              <el-checkbox :label="3">三线</el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37要居中长度</div>
          <div class="paramView">
            <span
              v-show="a37CenterLength"
              @click.stop="handlerSpace('a37CenterLength')"
              >{{ deviceParams.object.a37CenterLength }}</span
            >
            <el-input-number
              v-show="!a37CenterLength"
              size="mini"
              ref="a37CenterLength"
              v-model="deviceParams.object.a37CenterLength"
              @blur="handlerSpace('a37CenterLength')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37允许最大长度</div>
          <div class="paramView">
            <span
              v-show="a37MaxLength"
              @click.stop="handlerSpace('a37MaxLength')"
              >{{ deviceParams.object.a37MaxLength }}</span
            >
            <el-input-number
              v-show="!a37MaxLength"
              size="mini"
              ref="a37MaxLength"
              v-model="deviceParams.object.a37MaxLength"
              @blur="handlerSpace('a37MaxLength')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37允许剩余最小长度</div>
          <div class="paramView">
            <span
              v-show="a37MinRemainLength"
              @click.stop="handlerSpace('a37MinRemainLength')"
              >{{ deviceParams.object.a37MinRemainLength }}</span
            >
            <el-input-number
              v-show="!a37MinRemainLength"
              size="mini"
              ref="a37MinRemainLength"
              v-model="deviceParams.object.a37MinRemainLength"
              @blur="handlerSpace('a37MinRemainLength')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37玻璃间距</div>
          <div class="paramView">
            <span
              v-show="a37Interval"
              @click.stop="handlerSpace('a37Interval')"
              >{{ deviceParams.object.a37Interval }}</span
            >
            <el-input-number
              v-show="!a37Interval"
              size="mini"
              ref="a37Interval"
              v-model="deviceParams.object.a37Interval"
              @blur="handlerSpace('a37Interval')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">到达A38-1时间间隔</div>
          <div class="paramView">
            <span
              v-show="arriveA381Gap"
              @click.stop="handlerSpace('arriveA381Gap')"
              >{{ deviceParams.object.arriveA381Gap }}</span
            >
            <el-input-number
              v-show="!arriveA381Gap"
              size="mini"
              ref="arriveA381Gap"
              v-model="deviceParams.object.arriveA381Gap"
              @blur="handlerSpace('arriveA381Gap')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">到达A40-1时间间隔</div>
          <div class="paramView">
            <span
              v-show="arriveA401Gap"
              @click.stop="handlerSpace('arriveA401Gap')"
              >{{ deviceParams.object.arriveA401Gap }}</span
            >
            <el-input-number
              v-show="!arriveA401Gap"
              size="mini"
              ref="arriveA401Gap"
              v-model="deviceParams.object.arriveA401Gap"
              @blur="handlerSpace('arriveA401Gap')"
            ></el-input-number>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">A37允许停留最大时间</div>
          <div class="paramView">
            <span
              v-show="maxStayTime"
              @click.stop="handlerSpace('maxStayTime')"
              >{{ deviceParams.object.maxStayTime }}</span
            >
            <el-input-number
              v-show="!maxStayTime"
              size="mini"
              ref="maxStayTime"
              v-model="deviceParams.object.maxStayTime"
              @blur="handlerSpace('maxStayTime')"
            ></el-input-number>
          </div>
        </div>
        <div class="footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="handlerEditDevice">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import DCard from "@/views/components/production/dCard.vue";
import LogView from "@/views/components/production/logViews.vue";
import { deviceEditParam } from "@/api/assembly/device";
import { temperAfter } from "../Config/label";
import { mapState } from "vuex";
import mixin from "../mixin/index";
import { temperAfterDB } from "@/config/db";
import { cameraList } from "../Config/camera";
import Client from "@/views/production/device/components/client.vue";
export default {
  components: {
    DCard,
    LogView,
    Client,
  },
  mixins: [mixin],
  props: {},
  data() {
    return {
      cameraList,
      temperAfter,
      isCheckCard: 0,
      loading: false,
      isShow: false,
      tableData: [],
      device: null,
      loadDeviceParam: false,
      drawer: false,
      direction: "rtl",
      deviceParams: null,
      a37CenterLength: true,
      a37MaxLength: true,
      a37MinRemainLength: true,
      a37Interval: true,
      arriveA381Gap: true,
      arriveA401Gap: true,
      maxStayTime: true,
    };
  },
  methods: {
    //  index 选中的card
    handlerisCheck(item) {
      this.isCheckCard = item.value;
      this.device = item;
      this.getDeviceSignal();
    },
    async openParamSet() {
      this.loadDeviceParam = true;
      this.drawer = true;
      this.initData();
    },
    async initData() {
      this.loadDeviceParam = true;
      let paramList = this.temperAfter.map((item) => {
        return {
          name: item.deviceType,
          id: item.id,
        };
      });
      await this.$store.dispatch("getDeviceMap", paramList);
      if (this.deviceParamsList.length != 0) {
        this.deviceParams = this.deviceParamsList[this.isCheckCard];
        this.loadDeviceParam = false;
      }
    },
    handlerEditDevice() {
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^5563$/,
        inputErrorMessage: "密码错误",
        inputType: "password",
      })
        .then(({ value }) => {
          // this.deviceParams.name = "钢化后台子"‘
          this.deviceParams.object.canSendLine.sort((a, b) => b - a);

          deviceEditParam(this.deviceParams).then((res) => {
            if (res.code == 200) {
              this.$message.success("修改设备状态成功");
              this.drawer = false;
              this.initData();
            } else {
              this.$message.error("修改设备状态失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    handlerSpace(e) {
      this[e] = !this[e];
      this.$nextTick(() => {
        !this[e] && this.$refs[e].focus();
      });
    },
  },
  created() {
    this.device = temperAfter[this.isCheckCard];
    this.getSignal(temperAfterDB);
  },
  destroyed() {},
  computed: {
    ...mapState({
      deviceParamsList: (state) => state.deviceStatus.deviceParams,
      glassList: (state) => {
        return state.Glass.glass;
      },
    }),
    glassType() {
      let arr = [];
      let arr2 = [];
      let filterArr = [1144, 1170, 1171, 1172, 1173, 1174];
      if (this.isCheckCard == 0) {
        arr2 = this.glassList.filter((item) => item.place == 1144);
      }

      if (this.isCheckCard == 1) {
        arr2 = this.glassList.filter((item) =>
          filterArr.includes(item.place * 1)
        );
      }
      arr2.forEach((item) => {
        if (!arr.includes(item.type)) {
          arr.push(item.type);
        }
      });
      return arr;
    },
    showDeviceStatus() {
      if (this.deviceList.length == 0) return [];
      return this.deviceList[this.isCheckCard].plcSignals;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../style/index.less");
.device-img-rec {
  position: relative;
  height: 60%;
  display: flex;
  align-items: center;
  border-bottom: 1px dashed #e6e6e6;
  .title {
    font-size: 17px;
    font-weight: bold;
    padding-bottom: 5px;
    margin-bottom: 5px;
    border-bottom: 1px dashed #e6e6e6;
    display: flex;
    i {
      margin-left: auto;
      color: #409eff;
      transform: translateY(2px);
    }
    i:hover {
      cursor: pointer;
      text-decoration: underline;
    }
  }
  .img-box {
    flex: 1;
    display: flex;
    height: 100%;
    justify-content: center;
    align-items: center;
    position: relative;
    img {
      width: 90%;
      height: 98%;
    }
  }
}
</style>
